clear all
set more off
tempfile ubigeo temp

/* 

This dofile compiles all data files into the one with 94,786 observations used in the analysis.

The exposure to violence variables are defined for two pediors 80s y 90s.

Thus the difference wrt to mergedp.do is that this dofile pulls in violence variables that distincguish exposure to 80s and 90s violence.

Nomenclature for exposure variables follow the same system of AT, T and A, but defined for 80s and 90s

Again, it exposure variables by age groups: preschool, primary, secondary and tertiary
The overall measure if named "todo"

*/


** PATHS
local working ../working

** DATA
use `working'/300, clear
merge 1:1 conglome vivienda hogar codperso year using `working'/200
keep if _merge == 3
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/getsample
keep if _merge == 3
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/trust
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/identity
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/participation
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/democracy
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/voting
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceAT80
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceA80
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceT80
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceAT90
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceA90
drop if _merge == 2
drop _merge

merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceT90
drop if _merge == 2
drop _merge


recode events* (1/999 = 1)

gen prov1993 = int(ubigeo1993/100)
egen provid = group(prov1993)
*tab provid, gen(provid_)

egen trend = group(year)
replace trend = trend

recode part* (. = 0)

sum provid
local max = r(max)

forvalues p = 1/`max' {
	*replace provid_`p' = provid_`p' * trend
}	

foreach t in AT80 A80 T80 AT90 A90 T90 { 
	egen preesc`t' = rowtotal(events`t'1 events`t'2 events`t'3 events`t'4 events`t'5 events`t'6)
	egen primar`t' = rowtotal(events`t'7 events`t'8 events`t'9 events`t'10 events`t'11)
	egen second`t' = rowtotal(events`t'12 events`t'13 events`t'14 events`t'15 events`t'16 events`t'17 events`t'18)
	egen tertia`t' = rowtotal(events`t'19 events`t'20 events`t'21 events`t'22 events`t'23 events`t'24 events`t'25 events`t'26 events`t'27 events`t'28 events`t'29 events`t'30)

	gen todo`t' = preesc`t' + primar`t' + second`t' + tertia`t'
}

egen nomiss = rownonmiss(trust_*)
drop if nomiss == 0
drop nomiss

drop event*

** SAVE
compress
save `working'/mergedp8090, replace
